package com.zhaorujie.rpc.core.serialize.impl;

import com.google.gson.Gson;
import com.zhaorujie.rpc.core.serialize.RpcSerialize;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @author 赵汝杰
 * @version v1.0
 * @description JsonSerialize
 * @date 2024/3/4
 **/
public class JsonSerialize implements RpcSerialize {

    private static final Gson GSON;

    static {
        GSON = new Gson();
    }

    @Override
    public <T> byte[] serialize(T obj) {
        return GSON.toJson(obj).getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public <T> Object deserialize(byte[] bytes, Class<T> clazz) {
        return GSON.fromJson(new String(bytes, StandardCharsets.UTF_8), clazz);
    }
}
